<template>
	<view class="container">
		<!-- 自定义导航栏 -->
		<view class="custom-nav" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="nav-back" @click="goBack">
				<text class="back-icon">‹</text>
			</view>
			<view class="nav-title">选择车辆</view>
		</view>

		<scroll-view scroll-y class="content-scroll">
			<view class="vehicle-list">
				<view class="vehicle-card" v-for="(vehicle, index) in vehicleList" :key="index"
					:class="{ selected: selectedIndex === index }" @click="selectVehicle(index)">
					<image class="vehicle-image" :src="vehicle.image" mode="aspectFill"></image>
					<view class="vehicle-info">
						<view class="vehicle-model">车型: {{ vehicle.model }}</view>
						<view class="vehicle-plate">车牌: {{ vehicle.plate }}</view>
					</view>
					<view class="select-icon" v-if="selectedIndex === index">
						<text class="checkmark">✓</text>
					</view>
					<view class="select-icon empty" v-else></view>
				</view>
			</view>
		</scroll-view>

		<!-- 添加车辆按钮 -->
		<view class="add-btn" @click="addVehicle">
			<text class="add-icon">+</text>
			<text>添加车辆</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusBarHeight: 0,
				selectedIndex: 0,
				vehicleList: [{
						model: '奔驰-GR',
						plate: '豫M35468',
						image: '/static/image/touxiang.png'
					},
					{
						model: '宝马-GR',
						plate: '川M35468',
						image: '/static/image/yy.png'
					}
				]
			};
		},
		onLoad() {
			const systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight;
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			selectVehicle(index) {
				this.selectedIndex = index;
				// 返回上一页并传递选中的车辆信息
				const pages = getCurrentPages();
				const prevPage = pages[pages.length - 2];
				if (prevPage) {
					prevPage.$vm.orderInfo.vehicle = `${this.vehicleList[index].model}/${this.vehicleList[index].plate}`;
				}
				setTimeout(() => {
					uni.navigateBack();
				}, 300);
			},
			addVehicle() {
				// 跳转到添加车辆页面
				uni.navigateTo({
					url: '/pages/vehicle/add'
				});
			}
		}
	};
</script>

<style scoped>
	.container {
		width: 100%;
		height: 100vh;
		background-color: #F5F7FA;
		display: flex;
		flex-direction: column;
	}

	.custom-nav {
		height: 44px;
		display: flex;
		align-items: center;
		background-color: #4A90E2;
		position: relative;
	}

	.nav-back {
		width: 44px;
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.back-icon {
		color: white;
		font-size: 32px;
		font-weight: bold;
		line-height: 44px;
	}

	.nav-title {
		flex: 1;
		text-align: center;
		color: white;
		font-size: 18px;
		font-weight: 500;
	}

	.nav-right {
		width: 100px;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		padding-right: 15px;
	}

	.nav-icon {
		color: white;
		font-size: 18px;
		margin-left: 8px;
	}

	.content-scroll {
		flex: 1;
		padding: 15px;
	}

	.vehicle-list {
		display: flex;
		flex-direction: column;
	}

	.vehicle-card {
		background-color: white;
		border-radius: 10px;
		padding: 15px;
		margin-bottom: 15px;
		display: flex;
		align-items: center;
		position: relative;
	}

	.vehicle-card.selected {
		border: 2px solid #4A90E2;
	}

	.vehicle-image {
		width: 80px;
		height: 80px;
		border-radius: 40px;
		margin-right: 15px;
		background-color: #E0E0E0;
	}

	.vehicle-info {
		flex: 1;
	}

	.vehicle-model {
		font-size: 16px;
		font-weight: bold;
		color: #333;
		margin-bottom: 8px;
	}

	.vehicle-plate {
		font-size: 14px;
		color: #666;
	}

	.select-icon {
		width: 30px;
		height: 30px;
		border-radius: 15px;
		background-color: #FF9500;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.select-icon.empty {
		background-color: transparent;
		border: 2px solid #E0E0E0;
	}

	.checkmark {
		color: white;
		font-size: 20px;
		font-weight: bold;
	}

	.add-btn {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 50px;
		background-color: #4A90E2;
		color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 16px;
		font-weight: 500;
		border-radius: 25px 25px 0 0;
		margin: 0 15px;
	}

	.add-icon {
		font-size: 24px;
		margin-right: 8px;
		font-weight: bold;
	}
</style>